import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/index.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', hidden: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'HomeFilled' }
      }
    ]
  },
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/list',
    meta: { title: '商品管理', icon: 'ShoppingBag' },
    children: [
      {
        path: 'category',
        name: 'GoodsCategory',
        component: () => import('../views/goods/category.vue'),
        meta: { title: '商品分类' }
      },
      {
        path: 'list',
        name: 'GoodsList',
        component: () => import('../views/goods/list.vue'),
        meta: { title: '商品列表' }
      },
      {
        path: 'seckill',
        name: 'GoodsSeckill',
        component: () => import('../views/goods/seckill.vue'),
        meta: { title: '秒杀管理' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    children: [
      {
        path: 'list',
        name: 'OrderList',
        component: () => import('../views/order/index.vue'),
        meta: { title: '订单管理', icon: 'List' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/admin',
    meta: { title: '用户管理', icon: 'user' },
    children: [
      {
        path: 'admin',
        name: 'AdminManagement',
        component: () => import('@/views/user/admin.vue'),
        meta: { title: '管理员管理' }
      },
      {
        path: 'student',
        name: 'StudentUserManagement',
        component: () => import('@/views/user/studentUser.vue'),
        meta: { title: '学生用户管理' }
      }
    ]
  },
  {
    path: '/content',
    component: Layout,
    meta: { title: '内容管理', icon: 'Document' },
    children: [
      {
        path: 'comment',
        name: 'GoodsComment',
        component: () => import('../views/content/comment.vue'),
        meta: { title: '商品评论' }
      },
      {
        path: 'collect',
        name: 'GoodsCollect',
        component: () => import('../views/content/collect.vue'),
        meta: { title: '商品收藏' }
      },
      {
        path: 'messageWall',
        name: 'MessageWall',
        component: () => import('../views/content/messageWall.vue'),
        meta: { title: '留言管理' }
      }
    ]
  },
  {
    path: '/promotion',
    component: Layout,
    meta: { title: '促销管理', icon: 'Present' },
    children: [
      {
        path: 'coupon',
        name: 'PromotionCoupon',
        component: () => import('../views/system/coupon.vue'),
        meta: { title: '优惠券管理' }
      },
      {
        path: 'userCoupons',
        name: 'PromotionUserCoupons',
        component: () => import('../views/system/userCoupons.vue'),
        meta: { title: '用户优惠券' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: 'banner',
        name: 'SysBanner',
        component: () => import('../views/system/banner.vue'),
        meta: { title: '系统轮播图' }
      },
      {
        path: 'announcement',
        name: 'SysAnnouncement',
        component: () => import('../views/system/announcement.vue'),
        meta: { title: '公告管理' }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'Profile',
        component: () => import('@/views/user/profile.vue'),
        meta: { title: '个人信息', icon: 'User' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 纪念品商城管理系统` : '纪念品商城管理系统'
  
  // 验证是否需要登录
  const token = localStorage.getItem('token') || sessionStorage.getItem('token')
  
  if (to.path === '/login') {
    // 如果已登录且要去登录页，重定向到首页
    if (token) {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 其他页面需要验证登录
    if (!token) {
      // 如果没有token，强制跳转到登录页
      // 使用replace模式避免在历史记录中留下无效页面
      next({ path: '/login', replace: true })
    } else {
      next()
    }
  }
})

export default router 